package com.yict.csms.resourceplan.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yict.common.entity.PageEntity;
import com.yict.common.service.impl.CommonService;
import com.yict.csms.resourceplan.dao.impl.ShipAssignedDaoImpl;
import com.yict.csms.resourceplan.entity.ShipCsms;
import com.yict.csms.resourceplan.entity.ShipPlan;
import com.yict.csms.resourceplan.service.IShipAssignedService;

@Service
public class ShipAssignedServiceImpl extends CommonService<ShipPlan, Long> implements IShipAssignedService{

	@Autowired
	private ShipAssignedDaoImpl shipAssginedDaoImpl;
	
	public List<ShipPlan> search(Map<String, Object> queryMap,PageEntity page){
		pageEntity = new PageEntity();
		List<ShipPlan> list = new ArrayList<ShipPlan>();
		StringBuilder queryString = new StringBuilder();
		queryString.append("from ShipPlan as c where 1=1");
		if(queryMap!=null){
			if (queryMap.get("etbDate1") != null && !"".equals(queryMap.get("etbDate1"))) {
				queryString.append(" and c.ship.etb >= to_date(:etbDate1,'yyyy-MM-dd HH24:mi')");
			}
			if (queryMap.get("etbDate2") != null && !"".equals(queryMap.get("etbDate2"))) {
				queryString.append(" and c.ship.etb <= to_date(:etbDate1,'yyyy-MM-dd HH24:mi')");
			}
			if (queryMap.get("orderDate1") != null && !"".equals(queryMap.get("orderDate1"))) {
				queryString.append(" and c.orderDate >= to_date(:orderDate1,'yyyy-MM-dd')");
			}
			if (queryMap.get("orderDate2") != null && !"".equals(queryMap.get("orderDate2"))) {
				queryString.append(" and c.orderDate <= to_date(:orderDate2,'yyyy-MM-dd')");
			}
			if (queryMap.get("shipName") != null && !"".equals(queryMap.get("shipName"))) {
				queryString.append(" and upper(c.ship.shipName) like :shipName");
			}
			if (queryMap.get("voyage") != null && !"".equals(queryMap.get("voyage"))) {
				queryString.append(" and upper(c.ship.voyage) like :voyage");
			}
			if (queryMap.get("berth") != null && !"".equals(queryMap.get("berth"))) {
				queryString.append(" and upper(c.ship.berthId) like :berth");
			}
			if (queryMap.get("sendStatus") != null && !"".equals(queryMap.get("sendStatus"))) {
				queryString.append(" and upper(c.sendStatus.dictid) like :sendStatus");
			}
			if (queryMap.get("companyId") != null && !"".equals(queryMap.get("companyId"))) {
				queryString.append(" and c.company.companyId = :companyId");
			}
			if (queryMap.get("profession") != null && !"".equals(queryMap.get("profession"))) {
				queryString.append(" and upper(c.profession.dictid) like :profession");
			}
		}
		StringBuilder orderString = new StringBuilder("");
		if (page.getField() != null&&!"".equals(page.getField()) &&page.getOrder()!= null && !"".equals(page.getOrder())) {
			if(page.getField().equals("companySName")) {
				orderString.append(" c.company.companySName " + page.getOrder());
			} else if (page.getField().equals("voyage")){
				orderString.append(" c.ship.voyage " + page.getOrder());
			} else if (page.getField().equals("shipName")){
				orderString.append(" c.ship.shipName " + page.getOrder());
			} else if (page.getField().equals("qcList")){
				orderString.append(" c.ship.qcListe " + page.getOrder());
			} else if (page.getField().equals("berthId")){
				orderString.append(" c.ship.berthId " + page.getOrder());
			} else if (page.getField().equals("etb")){
				orderString.append(" c.ship.etb " + page.getOrder());
			} else if (page.getField().equals("tcc")){
				orderString.append(" c.ship.tcc " + page.getOrder());
			} else if (page.getField().equals("etd")){
				orderString.append(" c.ship.etd " + page.getOrder());
			} else if (page.getField().equals("sendStatusName")){
				orderString.append(" c.sendStatus.displayname " + page.getOrder());
			} else if (page.getField().equals("orderPersonName")){
				orderString.append(" c.orderPerson.userName " + page.getOrder());
			} else if (page.getField().equals("assignedPersonName")){
				orderString.append(" c.assignedPerson.userName " + page.getOrder());
			} else if (page.getField().equals("confirmPersonName")){
				orderString.append(" c.confirmPerson.userName " + page.getOrder());
			} else {
				orderString.append(page.getField() + page.getOrder());
			}
			queryString.append(" order by ").append(orderString);
		} else {
			queryString.append(" order by c.updatedate desc");
		}
		list = shipAssginedDaoImpl.search(queryString.toString(), queryMap, page);
		pageEntity.setTotalRecord(shipAssginedDaoImpl.getPageEntity().getTotalRecord());
		return list;
	}
	
	@Transactional(readOnly = false)
	public boolean update(List<ShipPlan> list) throws Exception {
		boolean bool = true;
		try {
			for (ShipPlan ship : list) {
				shipAssginedDaoImpl.update(ship);
			}
		} catch (Exception e) {
			bool = false;
			e.printStackTrace();
		}
		return bool;
	}
	
	public ShipPlan findById(Long id){
		return shipAssginedDaoImpl.findbyId(id);
	}
	
	public List<ShipPlan> findByShipId(Long shipId){
		String hql = "from ShipPlan c where c.ship.shipId = :shipId";
		HashMap<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("shipId", shipId);
		return shipAssginedDaoImpl.search(hql, valueMap, null);
	}
}
